import { Component, OnInit, NgModule, Input, Output } from '@angular/core';
import { ConmonService } from "../../service/common.service";
import { Router } from '@angular/router';
// declare var $: any;
import * as $ from 'jquery';
export class Audit {
  CheckOpin: string;
  SubmitNo: string;
  DismissReason: string;
}
@Component({
  selector: 'app-work-audit',
  templateUrl: './work-audit.component.html',
  styleUrls: ['./work-audit.component.scss']
})

@NgModule({
  providers: [
    ConmonService
  ]
})

export class WorkAuditComponent implements OnInit {
  private ismask: boolean = false;
  private tokenObj: any;
  private dataList: any;
  private SubmitNo: string;
  private pages: number = 1;
  private arrList: any;
  private totalPages: any;
  private domainVal: string = "afcat.com.cn";
  private url: string = "http://crmdev.afcat.com.cn:8001/psc/CRMDEV/EMPLOYEE/CRM/s/WEBLIB_SI_D.CRM_SI_HTTP.FieldFormula.IScript_CRM_Service";
  @Input() Audit: Audit = {
    CheckOpin: '',
    SubmitNo: '',
    DismissReason: '请填写驳回原因。。。'
  };
  //获取待审核报工信息列表
  constructor(private router: Router, private service: ConmonService) {
  };
  getWorkAuditData() {
    this.pages = 1;
    let dateObj = {
      AccessPages: this.pages
    };
    // this.tokenObj = this.service.getToken(1, dateObj, 'backlogListInfo', false);
    this.tokenObj = this.service.token;
    this.dataList = [];
    let totalData = this.service.withBodyData(dateObj, 'backlogListInfo');
    this.totalPages = totalData.body.totalPages;
    this.arrList = totalData.body.backlogListInfo;
    for (var key in this.arrList) {
      this.dataList.push(this.arrList[key]);
    };
    var arr = this.dataList;
    //按照报工日期排序进行展示
    for (var i = 0; i < arr.length - 1; i++) {
      for (var j = i + 1; j < arr.length; j++) {
        if (arr[i].WorkDate < arr[j].WorkDate) {
          var baba = arr[j];
          arr[j] = arr[i];
          arr[i] = baba;
        };
      };
    };
    console.log(this.tokenObj);
    console.log(this.dataList);
  };
  rejectedAgreed(val) {
    this.Audit.SubmitNo = val.target.childNodes[1].innerText;
    console.log(val.target.parentNode.parentNode.parentNode);
    if (val.target.innerText === "驳回") {
      this.ismask = true;
      //  向后台发请求
    } else {
      this.Audit.CheckOpin = '0';
      this.Audit.DismissReason = '';
      console.log(this.Audit);
      this.service.withBodyData(this.Audit, 'CheckDailyWork');
      this.getWorkAuditData();
      this.ismask = false;
      this.Audit.DismissReason = '请填写驳回原因。。。';
      //  向后台发请求
    }
  };
  scrollEvent(event) {
    // console.log(event.target.scrollTop);
    let _this = this;
    let scrollDiv = document.getElementById('WorkAudit');

    let oldScrollTop = scrollDiv.scrollTop;
    let scrollHeight = scrollDiv.scrollHeight;
    let clientHeight = scrollDiv.clientHeight;
    // console.log(oldScrollTop);
    // console.log(clientHeight);
    console.log(scrollHeight);
    if ((oldScrollTop + clientHeight >= scrollHeight) && (_this.pages < _this.totalPages)) {
      console.log(_this.pages, _this.totalPages);
      // _this.pages++;
      upCallback();
    }
    function upCallback() {
      // let _this = this;
      if (_this.pages >= _this.totalPages) {
        _this.pages = _this.totalPages;
      } else {
        _this.pages++;
      }
      console.log('上拉');
      // if (_this.pages <= _this.totalPages) {
      console.log(_this.pages);
      let dateObj = {
        AccessPages: _this.pages
      };
      // _this.tokenObj = _this.service.getToken(1, dateObj, 'backlogListInfo', true);
      _this.arrList = _this.service.withBodyData(dateObj, 'backlogListInfo').body.backlogListInfo;
      for (var key in _this.arrList) {
        _this.dataList.push(_this.arrList[key]);
      }
      var arr = _this.dataList;
      //按照报工日期排序进行展示
      for (var i = 0; i < arr.length - 1; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].WorkDate < arr[j].WorkDate) {
            var baba = arr[j];
            arr[j] = arr[i];
            arr[i] = baba;
          };
        };
      };
      // };
    };
  };
  public maskCancelSure(event): void {
    console.log(event.target)
    let eventCont = event.target.innerText;
    if (eventCont === '取消') {
      this.ismask = false;
      this.Audit.DismissReason = '请填写驳回原因。。。';
    }
    else if (eventCont === '确定') {
      this.Audit.CheckOpin = '1';
      this.service.withBodyData(this.Audit, 'CheckDailyWork');
      this.getWorkAuditData();
      this.ismask = false;
      this.Audit.DismissReason = '请填写驳回原因。。。';
      //  向后台发请求
    }
    // alert("收到了子组件的自定义事件！");
  };
  private page: Object = { num: 1, size: 10 };
  // 下拉刷新我的报工
  WorkAudit() {

  };
  ngOnInit() {
    this.getWorkAuditData();
    let _this = this;
    if (document.getElementById('WorkAudit')) {
      var mescroll = new MeScroll("WorkAudit", {
        down: {
          callback: downCallback //下拉刷新的回调,别写成downCallback(),多了括号就自动执行方法了
        }
        // up: {
        //   callback: upCallback
        // }
      });
    };
    //下拉刷新的回调
    function downCallback() {
      console.log('下拉');
      // _this.getWorkAuditData();
      //数组去重方法
      function unique5(array) {
        var r = [];
        for (var i = 0, l = array.length; i < l; i++) {
          for (var j = i + 1; j < l; j++)
            if (array[i].SubmitNo === array[j].SubmitNo) j = ++i;
          r.push(array[i]);
        }
        return r;
      };
      // let d = new Date();
      // d.setTime(d.getTime() + (5 * 1000));
      // let expires = "expires=" + d.toUTCString();
      // document.cookie = "ATOKEN=" + _this.tokenObj + ";domain=" + this.domainVal + ";path=/;" + expires + ";";
      $.ajax({
        url: _this.service.url,
        async: false, // 同步请求 false，异步请求 true
        type: "POST", // 请求类型，POST、GET
        xhrFields: {
          withCredentials: true
        },
        crossDomain: true,
        dataType: 'json',
        // data: { crmData: "{'head': {'ServName':'backlogListInfo','SeqNo':'123123123123123123123123','employId':'000123'},'body':{'AccessPages':'1'}}" },
        data: { crmData: "{'head': {'ServName':'backlogListInfo','SeqNo':'123123123123123123123123','employId':"+"'"+_this.service.employId+"'"+"},'body':{'AccessPages':'1'}}" },
        success: function (data) {
          console.log(data);
          //联网成功的回调,隐藏下拉刷新的状态;
          mescroll.endSuccess(); //无参
          //设置数据
          // _this.dataList = [];
          let totalData = data;
          _this.totalPages = totalData.body.totalPages;
          _this.arrList = totalData.body.backlogListInfo;
          //数组合并
          _this.dataList = _this.dataList.concat(_this.arrList);
          _this.dataList = unique5(_this.dataList);
          console.log(_this.dataList);
          var arr = _this.dataList;
          //按照报工日期排序进行展示
          for (var i = 0; i < arr.length - 1; i++) {
            for (var j = i + 1; j < arr.length; j++) {
              if (arr[i].WorkDate < arr[j].WorkDate) {
                var baba = arr[j];
                arr[j] = arr[i];
                arr[i] = baba;
              };
            };
          };
        },
        error: function (data) {
          //联网失败的回调,隐藏下拉刷新的状态
          mescroll.endErr();
        }
      });
    };
  };
}
